﻿namespace AR.Model
{
    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.ServiceModel.DomainServices.Client;
    using System.ServiceModel.DomainServices.Client.ApplicationServices;
    using AR.Entities;
    using AR.Web.Resources;
    using AR.Web.Services;
    //using AR.CientFramework;
    using System.Collections.ObjectModel;
    using AR.Web;
    using AR.CustomEntities;
    using AR.Models.Queries;
    using System.Windows;
    using System.Linq;
    using System.Collections.Generic;
    using AR.Helpers;
    using AR.Controls;
    /// <summary>
    /// This internal entity is used to ease the binding between the UI controls (DataForm and the label displaying a validation error) and the log on credentials entered by the user.
    /// </summary>
    public class EntretienTechniquetModel
    {
        public static EntitySet<THEME> Themescandidat = null;
        public readonly EntretienTechniqueContext EntretientechniqueContext;
        public EntretienTechniquetModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                EntretientechniqueContext = new EntretienTechniqueContext();

            }


        }

        public EntitySet<THEME> GetThemes(int idcandidat)
        {
            EntretientechniqueContext.THEMEs.Clear();
            EntretientechniqueContext.Load<THEME>(EntretientechniqueContext.GetTEST_TECHNIQUE_CANDIDAT2Query(idcandidat)).Completed +=(sender,arg) =>
            {
                (Application.Current.RootVisual as System.Windows.Controls.BusyIndicator).IsBusy = false;
            };
            return EntretientechniqueContext.THEMEs;


        }

        public EntitySet<REPONSE_TEST_TECHNIQUE_CANDIDAT> GetResultat(int idcandidat)
        {
          //  EntretientechniqueContext.REPONSE_TEST_TECHNIQUE_CANDIDATs.Clear();
            EntretientechniqueContext.Load<REPONSE_TEST_TECHNIQUE_CANDIDAT>(EntretientechniqueContext.GetREPONSE_TEST_TECHNIQUE_CANDIDATQuery());
            return EntretientechniqueContext.REPONSE_TEST_TECHNIQUE_CANDIDATs;
        
        }


        public void HandleTest(ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT> responses, ObservableCollection<QUESTIONS> Questions,int idcandidat,int Duree_test)
        {
            CandidatContext candidatcontext = new CandidatContext();
            int note = SetnoteTest(responses, Questions, idcandidat);
           
           
                foreach (var item in responses)
                {
                    EntretientechniqueContext.REPONSE_TEST_TECHNIQUE_CANDIDATs.Add(item);
                    
               
                }
                EntretientechniqueContext.SubmitChanges().Completed += (sender2, args2) =>
                {
                   
                             EntretientechniqueContext.SubmitChanges().Completed += (sender4, args4) => 
                             {
                                 var Candidatquery = candidatcontext.GetCandidatByIDQuery(idcandidat);
                                 candidatcontext.Load<CANDIDAT>(Candidatquery).Completed += (sender5, arg5) =>
                                 {
                                     var candidat = candidatcontext.CANDIDATs.SingleOrDefault();
                                     candidat.NOTE_TEST = int.Parse(note.ToString());
                                     candidat.DUREE_TEST = Duree_test;
                                     candidat.ETAT_ENTRETIEN_ECRIT = 1;
                                   //  candidat.ValidateEmployee(new ValidationContext(candidat));
                                     candidatcontext.SubmitChanges().Completed += (Sender, Args) =>
                                     {
                                         Tools.AfficheNotificationFlash("vos réponses ont été enregistrées,Merci.", 4);

                                         NavigationService.Navigate("/LoginPage");
                                     };


                                 };
                                 
                             };
                         };
        }

        private int SetnoteTest(ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT> responses, ObservableCollection<QUESTIONS> Questions,int idcandidat)
        {
            int GlobaleNote = 0;
            List<bool> validanswers;
            foreach (var question in Questions)
            {
                validanswers = new List<bool>(); ;
                bool validanswer = false;
                foreach (var choix in question.CHOIX)
                {
                    //si le choix et correct et le candidat
                    if (choix.IsCorrect)
                    {
                        if (responses.Where(_ => _.ID_CHOIX == choix.IDCHOIX && _.ID_CANDIDAT == idcandidat).Any())
                            validanswer = true;
                        else
                            validanswer = false;
                    }
                    else
                    {
                        if (responses.Where(_ => _.ID_CHOIX == choix.IDCHOIX && _.ID_CANDIDAT == idcandidat).Any())
                            validanswer = false;
                        else
                            validanswer = true;
                    }
                    validanswers.Add(validanswer);
                }
                if (validanswers.Where(x=>x==false).Any())
                    GlobaleNote = GlobaleNote - 1;
                else
                    GlobaleNote = GlobaleNote + 1;
            }
            return GlobaleNote;
        }
       
    }
}
